package cn.zwx.learn.spring.orm.demo.mapper;

import cn.zwx.learn.spring.orm.core.framework.BaseMapperSupport;
import cn.zwx.learn.spring.orm.core.framework.QueryRule;
import cn.zwx.learn.spring.orm.core.utils.Page;
import cn.zwx.learn.spring.orm.demo.entity.UserEntity;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * @author zhangwenxue
 */
@Repository
public class UserMapper extends BaseMapperSupport<UserEntity,Long> {


    @Resource(name="dataSource")
    @Override
    public void setDataSource(DataSource dataSource){
        super.setDataSourceWrite(dataSource);
    }


    @Override
    protected String getPkColumn() {
        return "id";
    }

    public List<UserEntity> selectAll(QueryRule queryRule) throws  Exception{
        return super.select(queryRule);
    }


    public Page<UserEntity> selectForPage(int pageNo, int pageSize,String name) throws Exception{
        QueryRule queryRule = QueryRule.getInstance();
        queryRule.andLike("username",name);
        queryRule.addDescOrder("create_time");
        return super.select(queryRule,pageNo,pageSize);
    }

    @Override
    public Long saveAndReturnId(UserEntity entity) throws Exception{
        return super.insertAndReturnId(entity);
    }

    @Override
    public boolean insert(UserEntity entity) throws Exception{
        return super.insert(entity);
    }

}
